//
//  M4NetworkPlug.swift
//  M4
//
//  Created by YuChuang on 2024/8/7.
//

import Foundation
import Moya


struct M4NetworkPlug: PluginType {

    private var M4isNeedLoading: Bool = true
    private var M4loading: M4LoadingAlertView?
    
    init(M4isNeedLoading: Bool) {
        self.M4isNeedLoading = M4isNeedLoading
        if M4isNeedLoading {
            M4loading = M4LoadingAlertView()
        }
    }
    
    func prepare(_ request: URLRequest, target: TargetType) -> URLRequest {
        return request
    }
    
   
    func willSend(_ request: RequestType, target: TargetType) {
        if M4isNeedLoading {
            DispatchQueue.main.async {
                M4loading?.M4LoadingAlertShow()
            }
        }
    }

   
    func didReceive(_ result: Result<Response, MoyaError>, target: TargetType) {
        DispatchQueue.main.async {
            M4loading?.M4AlertViewHidden()
        }
    }

    
    func process(_ result: Result<Response, MoyaError>, target: TargetType) -> Result<Response, MoyaError> {
        return result
    }

}
